/**
 * @Title: OrgController
 * @Package cn.com.yiqi.wsc.portal.controller.admin
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-胥钰淋
 * @date 2017/2/21 12:25
 * @version V1.0
 */
package cn.com.yiqi.wsc.portal.controller.admin;

import cn.com.yiqi.wsc.Ro.OrgVO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.order.criteria.OrderCriteria;
import cn.com.yiqi.wsc.dao.org.criteria.AdminOrgCriteria;
import cn.com.yiqi.wsc.entity.check.Check;
import cn.com.yiqi.wsc.entity.order.OrderMainDTO;
import cn.com.yiqi.wsc.entity.org.OrgAdminDTO;
import cn.com.yiqi.wsc.entity.org.OrgEntity;
import cn.com.yiqi.wsc.entity.org.OrgExtendEntity;
import cn.com.yiqi.wsc.entity.org.OrgLinkEntity;
import cn.com.yiqi.wsc.enumeration.CommonEnum;
import cn.com.yiqi.wsc.enumeration.OrgCheckStatus;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.portal.controller.admin.ro.OrgRO;
import cn.com.yiqi.wsc.service.org.OrgService;
import cn.com.yiqi.wsc.service.outputExcel.OutPutService;
import cn.com.yiqi.wsc.service.user.UserService;
import cn.com.yiqi.wsc.service.user.dataobject.UserDO;
import cn.com.yiqi.wsc.util.JsonUtil;
import com.alibaba.fastjson.JSON;
import com.nimbusds.jose.util.IntegerUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author 会员系统组-胥钰淋
 * @ClassName: OrgController
 * @Description: TODO
 * @date 2017/2/21 12:25
 */
@Controller
@RequestMapping("/org")
public class OrgController extends BaseController {

    @Autowired
    private OrgService orgService;

    @Autowired
    private UserService userService;
    @Autowired
    private OutPutService outPutService;

    /**
     * 页面跳转 index
     *
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("org/index");
        this.getUserData(mv);
        return mv;
    }

    /**
     * 页面跳转 添加
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("org/add");
        this.getUserData(mv);
        return mv;
    }

    /**
     * 页面跳转 详情
     *
     * @param orgId
     * @return
     */
    @RequestMapping("/detail")
    public ModelAndView detail(@RequestParam("orgId") Integer orgId, @RequestParam("addAccountFlag") Boolean addAccountFlag) {
        ModelAndView mv = new ModelAndView("org/detail");

        Result result = orgService.detail(orgId);


        mv.addObject("baseInfo", JsonUtil.toJSON(result.getData().get("baseInfo")));
        mv.addObject("extend", JsonUtil.toJSON(result.getData().get("extend")));
        mv.addObject("linkList", JsonUtil.toJSON(result.getData().get("linkList")));
        mv.addObject("accountList", JsonUtil.toJSON(result.getData().get("accountList")));
        mv.addObject("readonly", true);
        mv.addObject("addAccountFlag", addAccountFlag + "");

        this.getUserData(mv);

        return mv;
    }

    /**
     * 页面跳转 编辑
     *
     * @param orgId
     * @return
     */
    @RequestMapping("/edit")
    public ModelAndView edit(@RequestParam("orgId") Integer orgId, @RequestParam("addAccountFlag") Boolean addAccountFlag) {
        ModelAndView mv = new ModelAndView("org/detail");


        Result result = orgService.detail(orgId);
        mv.addObject("baseInfo", JsonUtil.toJSON(result.getData().get("baseInfo")));
        mv.addObject("extend", JsonUtil.toJSON(result.getData().get("extend")));
        mv.addObject("linkList", JsonUtil.toJSON(result.getData().get("linkList")));
        mv.addObject("accountList", JsonUtil.toJSON(result.getData().get("accountList")));
        mv.addObject("readonly", "false");
        mv.addObject("addAccountFlag", addAccountFlag + "");
        this.getUserData(mv);
        return mv;

    }

    /**
     * 查询商家列表
     *
     * @param criteria
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result pageList(@RequestBody AdminOrgCriteria criteria) {

        Result result = new Result();

        result = orgService.adminOrgInfoList(criteria);

        return result;
    }

    /**
     * 查询商家列表
     *
     * @param criteria
     * @return
     */
    @RequestMapping("/selectList")
    @ResponseBody
    public Result getOrgNameAndId(@RequestBody AdminOrgCriteria criteria) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        Result<List<OrgVO>> listResult = new Result<>();

        listResult = orgService.listOrgNameAndId(criteria.getName());

        if (ResultUtil.isSuccess(listResult)) {
            result.putData("list", listResult.getReturnValue());
        }

        return result;
    }


    /**
     * 添加商家
     *
     * @param ro
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insertOrg(@RequestBody OrgRO ro) {

        Result result = new Result();


        //做验证


        //默认审核中
        ro.getOrg().setStatus(OrgCheckStatus.ING.getValue());

        result = orgService.insert(ro.getOrg(), ro.getOrgExtend(), ro.getOrgLinkList(), userService.getCurrentUserId());

        if (ResultUtil.isSuccess(result)) {

        }

        return result;
    }


    private Result validation(OrgRO ro) {
        Result result = new Result();

        OrgEntity entity = ro.getOrg();

        OrgExtendEntity extendEntity = ro.getOrgExtend();

        if (StringUtils.isEmpty(entity.getName())) {
            return ResultUtil.paramError(result, "请输入名称");
        }

        if (StringUtils.isEmpty(entity.getCity())) {
            return ResultUtil.paramError(result, "请输入地址");
        }

        if (StringUtils.isEmpty(entity.getAddress())) {
            return ResultUtil.paramError(result, "请输入详细地址");
        }


        if (StringUtils.isEmpty(entity.getLinkName())) {
            return ResultUtil.paramError(result, "请输入联系人");
        }

        if (StringUtils.isEmpty(entity.getLinkTel())) {
            return ResultUtil.paramError(result, "请输入联系电话");
        }

        if (entity.getCharacter() == null) {
            return ResultUtil.paramError(result, "请选择企业性质");
        }

        if (StringUtils.isEmpty(extendEntity.getLicenseCode())) {
            return ResultUtil.paramError(result, "请");
        }

        return result;
    }


    /**
     * 修改商家
     *
     * @param orgEntity
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result updateOrg(@RequestBody OrgEntity orgEntity) {
        Result result = new Result();

        if (orgEntity.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }
        result = orgService.update(orgEntity, userService.getCurrentUserId());
        return result;
    }

    /**
     * 启用
     *
     * @param orgEntity
     * @return
     */
    @RequestMapping("/enable")
    @ResponseBody
    public Result enable(@RequestBody OrgEntity orgEntity) {
        Result result = new Result();

        if (orgEntity.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        OrgEntity entity = new OrgEntity();
        entity.setId(orgEntity.getId());
        entity.setEnable(CommonEnum.ENABLE.getValue());

        result = orgService.update(entity, userService.getCurrentUserId());

        return result;
    }

    /**
     * 禁用
     *
     * @param orgEntity
     * @return
     */
    @RequestMapping("/disable")
    @ResponseBody
    public Result disable(@RequestBody OrgEntity orgEntity) {
        Result result = new Result();

        if (orgEntity.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        OrgEntity entity = new OrgEntity();
        entity.setId(orgEntity.getId());
        entity.setEnable(CommonEnum.DISABLE.getValue());

        result = orgService.update(entity, userService.getCurrentUserId());

        return result;
    }


    /**
     * 删除商家
     *
     * @param orgEntity
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result deleteOrg(@RequestBody OrgEntity orgEntity) {
        Result result = new Result();

        if (orgEntity.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }
        OrgEntity orgEntityForDelete = new OrgEntity();
        orgEntityForDelete.setId(orgEntity.getId());
        orgEntityForDelete.setDelete(CommonEnum.DELETED.getValue());

        result = orgService.update(orgEntityForDelete, userService.getCurrentUserId());
        return result;
    }


    /**
     * 审核商家
     *
     * @param check
     * @return
     */
    @RequestMapping("/check")
    @ResponseBody
    public Result check(@RequestBody Check check) {
        Result result = new Result();

        result.setStatus(ResultStatusEnum.操作成功.getCode());

        result = orgService.check(check);

        return result;

    }

    /**
     * 修改营业信息
     *
     * @param extendEntity
     * @return
     */
    @RequestMapping("/extend/update")
    @ResponseBody
    public Result updateOrgExtend(@RequestBody OrgExtendEntity extendEntity) {
        Result result = new Result();

        if (extendEntity.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }
        result = orgService.updateExtend(extendEntity, userService.getCurrentUserId());

        return result;
    }

    /**
     * 修改联系人
     *
     * @param orgLinkEntity
     * @return
     */
    @RequestMapping("/link/update")
    @ResponseBody
    public Result updateOrgLink(@RequestBody OrgLinkEntity orgLinkEntity) {

        Result result = new Result();

        if (orgLinkEntity.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        result = orgService.updateLink(orgLinkEntity, userService.getCurrentUserId());

        return result;
    }

    /**
     * 添加联系人
     *
     * @param orgLinkEntity
     * @return
     */
    @RequestMapping("/link/add")
    @ResponseBody
    public Result addOrgLink(@RequestBody OrgLinkEntity orgLinkEntity) {

        Result result = new Result();

        result = orgService.insertLink(orgLinkEntity, userService.getCurrentUserId());

        return result;
    }


    /**
     * 添加联系人
     *
     * @param orgLinkEntity
     * @return
     */
    @RequestMapping("/link/delete")
    @ResponseBody
    public Result deleteOrgLink(@RequestBody OrgLinkEntity orgLinkEntity) {

        Result result = new Result();

        if (orgLinkEntity.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        result = orgService.deleteLink(orgLinkEntity);

        return result;
    }

    /**
     * 获取商家联系人列表
     *
     * @param orgLinkEntity
     * @return
     */
    @RequestMapping("/link/list")
    @ResponseBody
    public Result listOrgLink(@RequestBody OrgLinkEntity orgLinkEntity) {

        Result result = new Result();

        if (orgLinkEntity.getOrgId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        result = orgService.getOrgLinkListByOrgId(orgLinkEntity.getOrgId());

        return result;
    }


    /**
     * 查询账户接口
     *
     * @param criteria
     * @return
     */
    @RequestMapping("/account/list")
    @ResponseBody
    public Result accountList(@RequestBody AdminOrgCriteria criteria) {
        Result result = new Result();

        if (criteria.getOrgId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }

        result = orgService.getUserByOrgId(criteria);

        return result;
    }

    /**
     * 添加商家用户
     *
     * @param userDO
     * @return
     */
    @RequestMapping("/account/add")
    @ResponseBody

    public Result save(@RequestBody UserDO userDO) {
        Result result = new Result();


        if (userService.isExist(userDO.getTel())) {
            return ResultUtil.paramError(result, "手机号已存在");
        }


        result = orgService.addAccount(userDO, userService.getCurrentUserId());

        return result;
    }

    @RequestMapping("/outPut")
    public void outPut(@RequestParam(name = "name",required = false) String name,
                       @RequestParam(name = "city",required = false) String city,
                       @RequestParam(name = "saleAmountLow",required = false) Integer saleAmountLow,
                       @RequestParam(name = "saleAmountHigh",required = false) Integer   saleAmountHigh,
                       @RequestParam(name = "status",required = false) Integer status,
                       HttpServletRequest request, HttpServletResponse response) {

        AdminOrgCriteria criteria = new AdminOrgCriteria();
        criteria.setPageSize(500000);
        criteria.setName(name);
        criteria.setCity(city);
        criteria.setSaleAmountLow(saleAmountLow);
        criteria.setSaleAmountHigh(saleAmountHigh);
        criteria.setStatus(status);

        Result result = orgService.adminOrgInfoList(criteria);

        List<OrgAdminDTO> orgAdminDTOs =  (List<OrgAdminDTO>)orgService.adminOrgInfoList(criteria).getData().get("list");
        String title = "商家管理";
        String[] hearders = new String[]{"商家名称", "上级商家", "地区", "状态", "是否禁用(1启用0禁用)", "规模(人)", "销售额(￥)","创立时间"};//表头数组
        String[] fields = new String[]{"name", "parentName", "complateAddress", "status", "enable", "staffNumber", "saleAmount","establishTime"};//People对象属性数组
        String creater = userService.getCurrentUser().getRealName();
        try {
            outPutService.outPutExcel(orgAdminDTOs, title, hearders, fields, creater, request, response);
        } catch (Exception e) {
            e.printStackTrace();

        }

    }
}
